package a04_Volatile;

/**
 * @Author wait与synchronized的搭配使用，wait与notify必须是同一个锁对象
 * @Date 2023-05-05
 */
public class a02_Wait_Notify {

    private static Object locker = new Object();

    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            while (true){
                System.out.println("t1在等待之前");
                try {
                    synchronized (locker) {
                        locker.wait();
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("t1在等待之后");
                System.out.println("=============================");
            }
        });
        t1.start();
        Thread t2 = new Thread(()->{
            while (true){
                System.out.println("t2唤醒之前");
                //唤醒时也要使用同一个锁对象
                synchronized (locker) {
                    locker.notify();
                }
                System.out.println("t2唤醒之后");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t2.start();
    }
}
